package me.yricky.mayu.ui.tasks

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.Icon
import androidx.compose.material.LinearProgressIndicator
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import me.yricky.mayu.desktop.AppState
import me.yricky.mayu.ui.ItemTitleSp
import me.yricky.mayu.ui.horizontalSpacer
import me.yricky.mayu.ui.tasks.taskui.*

/**
 * @author Yricky
 * @date 2022/4/20
 */

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun Task.ContentItem(modifier: Modifier){
    Column(modifier) {
        Row(Modifier.fillMaxWidth(),verticalAlignment = Alignment.CenterVertically){
            Icon(Icons.Rounded.Timer,"",Modifier.size(32.dp))
            Text(name, fontSize = ItemTitleSp, modifier = Modifier.padding(4.dp), maxLines = 1)
        }
        Spacer(Modifier.horizontalSpacer())
        Row(Modifier.fillMaxWidth(),verticalAlignment = Alignment.CenterVertically) {
            Icon(
                when(status){
                    is Idle -> Icons.Rounded.NotStarted
                    is Running -> Icons.Rounded.RunCircle
                    is Terminate -> Icons.Rounded.RemoveCircle
                    is Finished -> Icons.Rounded.CheckCircle
                },
                ""
            )
            Row(Modifier.weight(1f)) {
                Row(Modifier.weight(1f)){
                    when(status){
                        is Idle -> {
                            Text("空闲")
                        }
                        is Running -> {
                            (status as? Running)?.let{
                                val progress by it.progress
                                LinearProgressIndicator(progress,Modifier.padding(4.dp).fillMaxWidth())
                            }
                        }
                        is Terminate -> {
                            Text("终止")
                        }
                        is Finished -> {
                            Text("已完成")
                        }
                    }
                }
                if(status !is Running){
                    Icon(Icons.Rounded.Delete,"",Modifier.clickable {
                        AppState.tasksListModel.list.remove(this@ContentItem)
                    })
                }
            }
        }
        AnimatedContent(status) {
            status.Page(Modifier.fillMaxWidth().padding(0.dp,8.dp,0.dp,0.dp))
        }

    }
}